package com.basic.common.utils.Excel;//package com.basic.common.utils.Excel;
//
//
//import com.basic.common.constant.PumpEquipmentDsConstant;
//import com.basic.system.dao.model.Dictionary;
//import com.basic.system.domain.DictionaryBo;
//import com.basic.system.service.DictionaryService;
//import org.apache.poi.ss.usermodel.*;
//import org.apache.poi.ss.util.CellRangeAddressList;
//import org.apache.poi.xssf.usermodel.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.util.Base64;
//import java.util.List;
//import java.util.concurrent.CompletableFuture;
//import java.util.concurrent.ExecutionException;
//
//
//
//@Component
//public class ExcelUtils {
//    @Autowired
//    private DictionaryService dictionaryService;
//     public static void main (String[] args){
//
//
//    }
//    /**
//     *
//     *
//     */
//    public  void testExcelDemo( HttpServletRequest request, HttpServletResponse response, String sheetName) throws ExecutionException, InterruptedException {
//        /** 第一步，创建一个Workbook，对应一个Excel文件  */
//        XSSFWorkbook wb = new XSSFWorkbook();
//        /** 第二步，在Workbook中添加一个sheet,对应Excel文件中的sheet  */
//        XSSFSheet sheet = wb.createSheet(sheetName);
//        /** 第三步，设置样式以及字体样式*/
//        XSSFCellStyle titleStyle = createTitleCellStyle(wb);
//        XSSFCellStyle headerStyle = createHeadCellStyle(wb);
//        XSSFCellStyle contentStyle = createContentCellStyle(wb);
//        /** 第四步，创建标题 ,合并标题单元格 */
//        // 行号
//        int rowNum = 0;
//        //第二行
//        XSSFRow row2 = sheet.createRow(rowNum++);
//        row2.setHeight((short) 700);
//        String[] row_third = PumpEquipmentDsConstant.getColumnList().toArray(new String[0]);
//        for (int i = 0; i < row_third.length; i++) {
//            sheet.setColumnWidth(i, 256*30); //设置列宽度
//            XSSFCell tempCell = row2.createCell(i);
//            tempCell.setCellValue(row_third[i]);
//            tempCell.setCellStyle(headerStyle);
////            if (i==0) {
////                tempCell.setCellStyle(createHeadCellStyle(wb, true));
////                sheet.setColumnWidth(i, 256*32);
////            }
////            if (i == 2 || i == 4 || i == 5 || i == 12 || i == 15) {
////                tempCell.setCellStyle(createHeadCellStyle(wb, true));
////            }
//
//        }
//        CompletableFuture<String[]> pumpStationList = null ;//getPumpStationList();
//        CompletableFuture<String[]> manufacturerList = getManufacturerList();
//        CompletableFuture<String[]> brandList = getBrandList();
//        CompletableFuture<String[]> productTypeList = getProductTypeList();
//        CompletableFuture.allOf(pumpStationList, manufacturerList, brandList, productTypeList).join();
//        if (manufacturerList.get() != null) {
//            String[] select = manufacturerList.get();
//            CellRangeAddressList region = new CellRangeAddressList(1, Short.MAX_VALUE,8, 8);
//            dropDownBox(sheet,select , region);
//        }
//        if (pumpStationList.get() != null) {
//            String[] select = pumpStationList.get();
//            CellRangeAddressList region = new CellRangeAddressList(1, Short.MAX_VALUE, 2, 2);
//            dropDownBox(sheet,select , region);
//        }
//
//        if (brandList.get() != null) {
//            String[] select = brandList.get();
//            CellRangeAddressList region = new CellRangeAddressList(1, Short.MAX_VALUE, 7, 7);
//            dropDownBox(sheet,select , region);
//        }
//        if (productTypeList.get() != null) {
//            String[] select = productTypeList.get();
//            CellRangeAddressList region = new CellRangeAddressList(1, Short.MAX_VALUE, 0, 0);
//            dropDownBox(sheet,select , region);
//        }
//        String[] productStatus = {"正常","报废"};
//        CellRangeAddressList region = new CellRangeAddressList(1, Short.MAX_VALUE, 12, 12);
//        dropDownBox(sheet,productStatus , region);
//
//
//       /* String filePath = "D:\\Demo\\";
//        String fileName = "testExcelDemo.xlsx";
//        File file = new File(filePath + fileName);
//        FileOutputStream fos = null;
//        try {
//            fos = new FileOutputStream(file);
//            // 写入磁盘
//            wb.write(fos);
//            fos.close();//记得关闭
//        } catch (IOException e) {
//            e.printStackTrace();
//        }*/
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 设置XLSX的内容类型
//        try {
//            // 获取浏览器名称
//            String agent = request.getHeader("user-agent");
//            String filename = sheetName + ".xlsx";
//            // 不同浏览器需要对文件名做特殊处理
//            if (agent.contains("Firefox")) {
//                // 火狐浏览器
////                filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?=";
//                String encodedFilename = Base64.getEncoder().encodeToString(filename.getBytes(StandardCharsets.UTF_8));
//                filename = "=?UTF-8?B?" + encodedFilename + "?=";
//                filename = filename.replaceAll("\r\n", "");
//            } else {
//                // IE及其他浏览器
//                filename = URLEncoder.encode(filename, "utf-8");
//                filename = filename.replace("+", " ");
//            }
//            response.setHeader("Access-Control-Allow-Origin", "*");
//            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
//            // 推送到浏览器
//            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
//            wb.write(response.getOutputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * 创建标题样式
//     * @param wb
//     * @return
//     */
//    private static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) {
//        XSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
//        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐
//        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景颜色
//
//        XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 创建字体样式
//        headerFont1.setBold(true); //字体加粗
//        headerFont1.setFontName("黑体"); // 设置字体类型
//        headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
//        cellStyle.setFont(headerFont1); // 为标题样式设置字体样式
//        return cellStyle;
//    }
//
//    /**
//     * 创建表头样式
//     * @param wb
//     * @return
//     */
//    private static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) {
//        XSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setWrapText(true);// 设置自动换行
////        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景颜色
////        if (isWarning){
////            cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
////        }
//        cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
//        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直对齐
//        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
//        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
//        cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
//        cellStyle.setBorderRight(BorderStyle.THIN); //右边框
//        cellStyle.setBorderTop(BorderStyle.THIN); //上边框
//
//        XSSFFont headerFont = (XSSFFont) wb.createFont(); // 创建字体样式
//        headerFont.setBold(true); //字体加粗
//        headerFont.setFontName("黑体"); // 设置字体类型
//        headerFont.setFontHeightInPoints((short) 12); // 设置字体大小
//        cellStyle.setFont(headerFont); // 为标题样式设置字体样式
//
//        return cellStyle;
//    }
//
//    /**
//     * 创建内容样式
//     * @param wb
//     * @return
//     */
//    private static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) {
//        XSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
//        cellStyle.setWrapText(true);// 设置自动换行
//        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
//        cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
//        cellStyle.setBorderRight(BorderStyle.THIN); //右边框
//        cellStyle.setBorderTop(BorderStyle.THIN); //上边框
//        // 生成12号字体
//        XSSFFont font = wb.createFont();
//        font.setColor((short)8);
//        font.setFontHeightInPoints((short) 12);
//        cellStyle.setFont(font);
//        return cellStyle;
//    }
//
//    /**
//     * 单元格设置下拉框格式
//     */
//    public void dropDownBox(XSSFSheet sheet,String[] datas,CellRangeAddressList regions) {
//        DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
//        DataValidationConstraint createExplicitListConstraint = dataValidationHelper.createExplicitListConstraint(datas);
//        DataValidation createValidation = dataValidationHelper.createValidation(createExplicitListConstraint, regions);
//
//        if (createValidation instanceof XSSFDataValidation) {
//            createValidation.setSuppressDropDownArrow(true);
//            createValidation.setShowErrorBox(true);
//        } else {
//            createValidation.setSuppressDropDownArrow(false);
//        }
//        sheet.addValidationData(createValidation);
//    }
//
////    /***
////     * 异步获取集合
////     * @return
////     */
////    @Async("firstExecutor")
////    public CompletableFuture<String[]> getPumpStationList() {
////        List<PumpStationData> pumpStationData = pumpStationDataService.selPumpStationDataListByParam(new PumpStationDataBo());
////        if (CollectionUtils.isEmpty(pumpStationData)) {
////            return CompletableFuture.completedFuture(new String[]{});
////        }
////        String[] array = pumpStationData.stream().map(PumpStationData::getFullname).toArray(String[]::new);
////        return CompletableFuture.completedFuture(array);
////    }
//
//    /***
//     * 异步获取设备类型
//     * @return
//     */
//    @Async("firstExecutor")
//    public CompletableFuture<String[]> getProductTypeList() {
//        DictionaryBo dictionaryBo = new DictionaryBo();
//        dictionaryBo.setDictId("device_type");
//        List<Dictionary> dictionaries = dictionaryService.selectByExample(dictionaryBo);
//        if (CollectionUtils.isEmpty(dictionaries)) {
//            return CompletableFuture.completedFuture(new String[]{});
//        }
//        String[] array = dictionaries.stream().map(Dictionary::getValue).toArray(String[]::new);
//        return CompletableFuture.completedFuture(array);
//    }
//
//    /***
//     * 异步获取厂商
//     * @return
//     */
//    @Async("firstExecutor")
//    public CompletableFuture<String[]> getManufacturerList() {
//        DictionaryBo dictionaryBo = new DictionaryBo();
//        dictionaryBo.setDictId("manufacturer_code");
//        List<Dictionary> dictionaries = dictionaryService.selectByExample(dictionaryBo);
//        if (CollectionUtils.isEmpty(dictionaries)) {
//            return CompletableFuture.completedFuture(new String[]{});
//        }
//        String[] array = dictionaries.stream().map(Dictionary::getValue).toArray(String[]::new);
//        return CompletableFuture.completedFuture(array);
//    }
//
//    /***
//     * 异步获取品牌
//     * @return
//     */
//    @Async("firstExecutor")
//    public CompletableFuture<String[]> getBrandList() {
//        DictionaryBo dictionaryBo = new DictionaryBo();
//        dictionaryBo.setDictId("brand_code");
//        List<Dictionary> dictionaries = dictionaryService.selectByExample(dictionaryBo);
//        if (CollectionUtils.isEmpty(dictionaries)) {
//            return CompletableFuture.completedFuture(new String[]{});
//        }
//        String[] array = dictionaries.stream().map(Dictionary::getValue).toArray(String[]::new);
//        return CompletableFuture.completedFuture(array);
//    }
//
//}
